import java.util.ArrayList;
import java.util.List;
import java.io.*;

public class UserList {
    private static List<Movie> movieList = new ArrayList<>();  // 影片列表
    private static List<Hall> hallList = new ArrayList<>();    // 放映厅列表
    private static List<Order> orderList = new ArrayList<>();  // 订单列表
    private static List<Show> showList = new ArrayList<>();
    private static List<User> users = new ArrayList<>();
    private static final String USER_FILE = "users.txt";
    static {
        loadFromFile();
    }
    private static void loadFromFile() {
        File file = new File(USER_FILE);
        if (!file.exists()) {
            users.add(Admin.DEFAULT_ADMIN);
            saveToFile();
            return;
        }
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            br.readLine();
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length == 6) {
                    String userId = parts[0];
                    String userName = parts[1];
                    String password = parts[2];
                    String registerTime = parts[3];
                    String phone = parts[4];
                    String userType = parts[5];

                    User user = null;
                    switch (userType) {
                        case "管理员":
                            user = new Admin(userId, userName, password, registerTime, phone, userType);
                            break;
                        case "经理":
                            user = new Manager(userId, userName, password, registerTime, phone, userType);
                            break;
                        case "前台":
                            user = new FrontDesk(userId, userName, password, registerTime, phone, userType);
                            break;
                    }
                    if (user != null) users.add(user);
                }
            }
        } catch (IOException e) {
            System.out.println("加载用户失败：" + e.getMessage());
        }
    }
    public static void saveToFile() {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(USER_FILE))) {
            bw.write("用户ID,用户名,密码,注册时间,手机号,用户类型\n");
            for (User u : users) {
                String line = String.join(",", u.getUserId(), u.getUserName(), u.getUserPassword(), u.getRegisterTime(), u.getPhone(), u.getUserType());
                bw.write(line + "\n");
            }
        } catch (IOException e) {
            System.out.println("保存用户失败：" + e.getMessage());
        }
    }
    public static List<User> getAllUsers() {
        return new ArrayList<>(users);
    }
    public static void addUser(User user) {
        users.add(user);
        saveToFile();
        Log.info("系统", "添加用户", "用户名：" + user.getUserName() + "，类型：" + user.getUserType());
    }
    public static void removeUser(User user) {
        users.remove(user);
        saveToFile();
        Log.info("系统", "删除用户", "用户名：" + user.getUserName() + "，类型：" + user.getUserType());
    }
}
